

package com.zy.controller;

import com.zy.entity.Menu;
import com.zy.entity.vo.MenuIds;
import com.zy.service.MenuService;
import com.zy.util.R;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping({"menu"})
public class MenuController {
    @Resource
    MenuService menuService;


    @GetMapping({"getById/{id}"})
    public R getById(@PathVariable Integer id) {
        Menu menu = this.menuService.getById(id);
        return R.ok().put("menu", menu);
    }

    @PutMapping({"save"})
    public R save(@RequestBody Menu menu) {
        this.menuService.save(menu);
        return R.ok();
    }

    @PostMapping({"updateById"})
    public R updateById(@RequestBody Menu menu) {
        this.menuService.updateById(menu);
        return R.ok();
    }

    @GetMapping({"tree"})
    public R tree() {
        List<Menu> menuTree = this.menuService.tree();
        return R.ok().put("menuTree", menuTree);
    }

    @DeleteMapping({"delete/{id}"})
    public R delete(@PathVariable Integer id) {
        this.menuService.delete(id);
        return R.ok();
    }
    @GetMapping({"getByRoleId/{roleId}"})
    public R getByRoleId(@PathVariable Integer roleId) {
        List<Menu> menuList = this.menuService.getTreeByRoleId(roleId);
        return R.ok().put("menuList", menuList);
    }
    @PostMapping({"setByRoleId/{roleId}"})
    public R setByRoleId(@RequestBody MenuIds menuIds,  @PathVariable Integer roleId) {
        this.menuService.setByRoleId(menuIds.getMenuIds(),roleId);
        return R.ok();
    }
}
